//
//  大鱼吃小鱼.swift
//  iOSAlgorithm
//
//  Created by Mr.Wei on 2021/5/6.
//

import Foundation

//给定两个数组，size 表示大小 derection 表示鱼运动方向(0左 1右)
//两鱼相遇后   小鱼会被吃
//问最终剩几条鱼

func fish(sizes:[Int],derections:[Int]) -> Int {
    let num = sizes.count
    if num<=1 {
        return num
    }
    var stack = Stack<Int>()
    for i in 0..<sizes.count {
        let size = sizes[i]
        let derection = derections[i];
        var last = stack.top
        
        while last != nil {
            let lastD = derections[last!]
            let lastS = sizes[last!]
            var eat = false
            if lastD == 1 && derection == 0 {
                if lastS > size  {
                    eat = true
                }else{
                    stack.pop()
                    last = stack.top
//                    stack.push(i)
                    continue
                }
            }
            if eat == false {
                stack.push(i)
            }
            break
        }
        if last == nil {
            stack.push(i)
            continue
        }
    }
    return stack.count;
}
